package com.jiang.Controller;

import com.jiang.Pojo.Person;
import com.jiang.Service.PersonService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.List;

@Controller
public class PersonController {
    @Autowired
    PersonService personService;

    @Qualifier("person1")

    @RequestMapping("/person")
    public Person getPersonbyId(int id) {
        return personService.getPersonbyId(id);
    }

    @RequestMapping("/test")
    public void test(int id) {
        Person person = personService.getPersonbyId(id);
        System.out.println(person);
        //http://localhost:8080/test?id=1
    }

    @RequestMapping("/addperson")
    public int addPerson() {
        Person person1 = new Person();
        person1.setAge(22);
        person1.setName("张海");
        return personService.addPerson(person1);
    }

    @RequestMapping("/deletePerson")
    public int deletePerson() {
        int id = 3;
        return personService.deletePerson(id);
    }

    @ResponseBody
    @RequestMapping("/getAllPerson")
    public List<Person> getAllPerson() {
        return personService.getAllPerson();
    }


    @RequestMapping("/getAllPersons")
    public String list(Model model) {
        List<Person> list = personService.getAllPerson();
        model.addAttribute("list", list);
        return "allPerson";
    }

    @RequestMapping("/")
    public String tests() {
        return "index";
    }


    @RequestMapping("/tests")
    public void test() {
        List<Person> person = personService.getAllPerson();
        System.out.println(person);
        //http://localhost:8080/tests
    }

    @RequestMapping("/updatePerson")
    public int updatePerson() {
        Person person2 = new Person();
        person2.setId(1);
        person2.setAge(29);
        person2.setName("将涛");
        return personService.updatePerson(person2);
        //http://localhost:8080/updatePerson
    }

}
